• Jump To … +
    main.js separate.js single.js web-apg-api.js main.js web-conv-api.js ast.js csv.js dangling-else.js display.js flags.js float.js limits.js main.js multiline-mode.js recursive.js replace.js rules.js split.js testonly.js trace.js udt.js unicode.js web-email.js word-boundaries.js main.js phone-number.js web-main.js web-phone-number.js main.js phone-number.js setup.js translate.js xml.js branch-fail-grammar.js main.js parent-mode-grammar.js setup.js universal-mode-grammar.js colors-app.js colors-callbacks.js colors.js main.js more-app.js more-setup.js more.js ast-callbacks.js bad-input.js basic.js ini-file.js main.js parser-callbacks.js setup.js trace.js anbncn.js and.js c-comment.js compound.js main.js nested.js not.js setup.js boundaries-grammar.js boundaries.js comment-grammar.js comment.js main.js negative-grammar.js negative.js positive-grammar.js positive.js setup.js main.js odata-grammar.js run.js setup.js area-code.js lookaround.js main.js phone-number.js setup.js simple.js all-operators.js default.js fancy-number.js limited-lines.js main.js select-operators.js select-rules.js setup.js main.js minimal.js parent-u.js parent.js phone-number.js setup.js stats.js trace.js universal-u.js universal.js callbacks.js grammar.js main.js parser.js writeHtml.js LICENSE.md README.md index.md
  • colors-callbacks.js

  • §
    /* eslint-disable func-names */
    /*  *************************************************************************************
     *   copyright: Copyright (c) 2021 Lowell D. Thomas, all rights reserved
     *     license: BSD-2-Clause (https://opensource.org/licenses/BSD-2-Clause)
     *   ********************************************************************************* */
  • §

    The callback functions for the colors-app application. For the most part, these functions simply write the matched phrases to the console.

    'use strict;';
    
    const { apgLib } = require('apg-js');
    
    const id = apgLib.ids;
  • §

    The start rule. Initialize the data object.

    exports.startCallback = function (result, chars, phraseIndex, data) {
      switch (result.state) {
        case id.ACTIVE:
          console.log('startCallback: start up: ACTIVE');
          data.length = 0;
          break;
        case id.EMPTY:
          console.log('startCallback: done: EMPTY');
          break;
        case id.MATCH:
          console.log('startCallback: done: MATCH');
          break;
        case id.NOMATCH:
          console.log('startCallback: done: NOMATCH');
          break;
        default:
          throw new Error('unrecognozed state');
      }
      if (result.state !== id.ACTIVE) {
        console.log('startCallback: done: data found');
        data.forEach((dat) => {
          console.log(`startCallback: ${dat}`);
        });
      }
    };
  • §

    The color callback for the monitoring-only demonstration.

    exports.monitorCallback = function (result, chars, phraseIndex, data) {
      switch (result.state) {
        case id.ACTIVE:
          console.log('colorCallback: monitor only: just note the state of the call');
          console.log('colorCallback: monitor only: ACTIVE');
          break;
        case id.EMPTY:
          console.log('colorCallback: monitor only: EMPTY');
          break;
        case id.MATCH:
          console.log('colorCallback: monitor only: MATCH');
          data.push(apgLib.utils.charsToString(chars, phraseIndex, result.phraseLength));
          break;
        case id.NOMATCH:
          console.log('colorCallback: monitor only: NOMATCH');
          break;
        default:
          throw new Error('unrecognozed state');
      }
    };
  • §

    The color callback for the parsing modification demonstration.

    exports.modifyCallback = function (result) {
      switch (result.state) {
        case id.ACTIVE:
          console.log('colorCallback: modify the result: fail no matter what the input is');
  • §

    By returning the NOMATCH state, indicate that no phrase was matched. Note that by returning any non-ACTIVE state, this RNM operator behaves exactly like a UDT. That is it matches any phrase the user likes, ignoring the phrases defined by the SABNF grammar. In fact, UDTs were originally developed to clean up the clumsiness of doing it this way.

          result.state = id.NOMATCH;
          result.phraseLength = 0;
          break;
        case id.EMPTY:
          console.log('colorCallback: modify: EMPTY: should never see this');
          break;
        case id.MATCH:
          console.log('colorCallback: modify: MATCH: should never see this');
          break;
        case id.NOMATCH:
          console.log('colorCallback: modify: NOMATCH: should never see this');
          break;
        default:
          throw new Error('unrecognozed state');
      }
    };
  • §

    The color callback for calling a UDT from a rule name.

    exports.callUdtCallback = function (result, chars, phraseIndex, data) {
      const TRUE = true;
      switch (result.state) {
        case id.ACTIVE:
          while (TRUE) {
  • §

    Calls the UDT with index 0 (the first one defined in the grammar - see colors.js)

            result.evaluateUdt(0, phraseIndex, result);
            if (result.state === id.MATCH) {
              break;
            }
  • §

    Calls the UDT with index 1.

            result.evaluateUdt(1, phraseIndex, result);
            if (result.state === id.MATCH) {
              break;
            }
  • §

    Calls the UDT with index 2.

            result.evaluateUdt(2, phraseIndex, result);
            if (result.state === id.MATCH) {
              break;
            }
  • §

    Calls the UDT with index 3.

            result.evaluateUdt(3, phraseIndex, result);
            if (result.state === id.MATCH) {
              break;
            }
            break;
          }
          if (result.state === id.MATCH) {
            console.log('colorCallback: call evaluateUdt() MATCH');
            data.push(apgLib.utils.charsToString(chars, phraseIndex, result.phraseLength));
          } else {
            console.log('colorCallback: call evaluateUdt() NOMATCH');
          }
          break;
        case id.EMPTY:
          console.log('colorCallback: evalutateUdt(): EMPTY: should never see this');
          break;
        case id.MATCH:
          console.log('colorCallback: evalutateUdt(): MATCH: should never see this');
          break;
        case id.NOMATCH:
          console.log('colorCallback: evalutateUdt(): NOMATCH: should never see this');
          break;
        default:
          throw new Error('unrecognozed state');
      }
    };
  • §

    The UDT callback functions:

    exports.u_redCallback = function (result, chars, phraseIndex) {
      result.state = id.NOMATCH;
      result.phraseLength = 0;
      if (phraseIndex + 3 <= chars.length) {
        if (chars[phraseIndex] === 114 && chars[phraseIndex + 1] === 101 && chars[phraseIndex + 2] === 100) {
          result.state = id.MATCH;
          result.phraseLength = 3;
        }
      }
    };
    exports.u_whiteCallback = function (result, chars, phraseIndex) {
      result.state = id.NOMATCH;
      result.phraseLength = 0;
      if (phraseIndex + 5 <= chars.length) {
        if (
          chars[phraseIndex] === 119 &&
          chars[phraseIndex + 1] === 104 &&
          chars[phraseIndex + 2] === 105 &&
          chars[phraseIndex + 3] === 116 &&
          chars[phraseIndex + 4] === 101
        ) {
          result.state = id.MATCH;
          result.phraseLength = 5;
        }
      }
    };
    exports.u_blueCallback = function (result, chars, phraseIndex) {
      result.state = id.NOMATCH;
      result.phraseLength = 0;
      if (phraseIndex + 4 <= chars.length) {
        if (
          chars[phraseIndex] === 98 &&
          chars[phraseIndex + 1] === 108 &&
          chars[phraseIndex + 2] === 117 &&
          chars[phraseIndex + 3] === 101
        ) {
          result.state = id.MATCH;
          result.phraseLength = 4;
        }
      }
    };
    exports.u_yellowCallback = function (result, chars, phraseIndex) {
      result.state = id.NOMATCH;
      result.phraseLength = 0;
      if (phraseIndex + 6 <= chars.length) {
        if (
          chars[phraseIndex] === 121 &&
          chars[phraseIndex + 1] === 101 &&
          chars[phraseIndex + 2] === 108 &&
          chars[phraseIndex + 3] === 108 &&
          chars[phraseIndex + 4] === 111 &&
          chars[phraseIndex + 5] === 119
        ) {
          result.state = id.MATCH;
          result.phraseLength = 6;
        }
      }
    };